﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WJP.Dependency
{
    /// <summary>
    /// <see cref="IIocRegistrar"/>接口的扩展方法
    /// </summary>
    public static class IocRegistrarExtensions
    {
        /// <summary>
        /// 如果未注册则进行注册
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="iocRegistrar"></param>
        /// <param name="lifeStyle"></param>
        /// <returns></returns>
        public static bool RegisterIfNot<T>(this IIocRegistrar iocRegistrar, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
        {
            return iocRegistrar.RegisterIfNot(typeof(T), lifeStyle);
        }

        /// <summary>
        /// 如果未注册则进行注册
        /// </summary>
        /// <param name="iocRegistrar"></param>
        /// <param name="type"></param>
        /// <param name="lifeStyle"></param>
        /// <returns></returns>
        public static bool RegisterIfNot(this IIocRegistrar iocRegistrar, Type type, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
        {
            if (iocRegistrar.IsRegistered(type))
            {
                return false;
            }

            iocRegistrar.Register(type, lifeStyle);
            return true;
        }

        /// <summary>
        /// 如果未注册则注册类型的实现
        /// </summary>
        /// <typeparam name="TType"></typeparam>
        /// <typeparam name="TImpl"></typeparam>
        /// <param name="iocRegistrar"></param>
        /// <param name="lifeStyle"></param>
        /// <returns></returns>
        public static bool RegisterIfNot<TType, TImpl>(this IIocRegistrar iocRegistrar, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
        {
            return iocRegistrar.RegisterIfNot(typeof(TType), typeof(TImpl), lifeStyle);
        }

        /// <summary>
        /// 如果未注册则注册类型的实现
        /// </summary>
        /// <param name="iocRegistrar"></param>
        /// <param name="type"></param>
        /// <param name="impl"></param>
        /// <param name="lifeStyle"></param>
        /// <returns></returns>
        public static bool RegisterIfNot(this IIocRegistrar iocRegistrar, Type type, Type impl, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
        {
            if (iocRegistrar.IsRegistered(type))
            {
                return false;
            }

            iocRegistrar.Register(type, impl, lifeStyle);
            return true;
        }
    }
}
